//bench.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({
  data: {
    formData: {},
    equipCode: '',
    equipLocationInfo: {},
    changedLocationInfo: {}
  },
  chooseHospital: function(){
    wx.navigateTo({
      url: '/pages/chooseHospital/chooseHospital',
    })
  },
  changeEquipLocation: function (){
    let that = this;
    let formData = that.data.formData
    let equipChangeLog = {
      changeDate: new Date(),
      equipCode: that.data.equipCode,
      equipLocationInfo: that.data.equipLocationInfo,
      changedLocationInfo: that.data.changedLocationInfo,
      equipChangeReason: formData.equipChangeReason
    }
    console.log(equipChangeLog)
    lbbService.changeEquipLocation({
        position:'',
        equId: parseInt(that.data.equipCode.replace(/[^0-9]/ig,"")),
        lbmid: parseInt(that.data.changedLocationInfo.hospitalId),
        equType: that.data.categoryId,
        imgs: '',
        reason: formData.equipChangeReason,
      },function(res){
        if (res == -1) {
          wx.showToast({
            image: '/images/gantan.png',
            title: '租赁中不可变更!',
            duration: 2500
          })
        } else {
          wx.showToast({
            title: '变更成功',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }




      })
  },
  formSubmit: function(e){
    let that = this;
    let formData = e.detail.value
    that.setData({
      formData: formData
    })
    let modalText = '确定将设备L' + that.data.equipCode + '变更到' + that.data.changedLocationInfo.hospitalName
    that.personalModal.showModal(modalText, that.changeEquipLocation)
  },
  onLoad: function (options) {
    let that = this
    console.log(options)
    that.data.categoryId = options.categoryId;
    let equipLocationInfo = {
      hospitalId: options.hospitalId,
      hospitalName: options.hospitalName,
    }
    let changedLocationInfo = {
      hospitalId: options.hospitalId,
      hospitalName: options.hospitalName
    }
    that.setData({
      equipCode: options.equipCode,
      equipType: options.equipType=='担架'?2:1,
      equipLocationInfo: equipLocationInfo,
      changedLocationInfo: changedLocationInfo,
      scrollHeight:app.getScrollHeight(0)
    })
    console.log(that.data)
  },
  onReady: function () {
    this.personalModal = this.selectComponent("#personalModal")
    // Do something when page ready.
  },
  onShow: function () {
    let that = this
    let equType = that.data.categoryId ? that.data.categoryId : parseInt(that.data.equipType);
    lbbService.getEquChangeInfo({
        hospitalId: parseInt(that.data.changedLocationInfo.hospitalId),
        equType: equType,
        equId: that.data.equipCode
      }, function (res){
        
        let categoryNameArr = [];
        let categoryList = res.categoryList;
        let equCid = res.equCid;
        let categoryIndex = 0;
        if(categoryList) {
            categoryList.forEach((el,i)=>{
                categoryNameArr.push(el.name);
                if(el.categoryId == equCid) {
                    categoryIndex = i;
                    that.data.categoryId = el.categoryId                    
                }
            })
        }

        that.setData({
          feeRules: res.data,
          categoryIndex: categoryIndex,
          categoryInfoList: categoryList,
          categoryNameArr: categoryNameArr
        })
      })
    
  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },
  onPullDownRefresh: function () {
    // Do something when pull down
  },
  // Event handler.

    
  bindCategoryPickerChange:function(e) {
    this.setData({
        categoryIndex: e.detail.value
    })
    this.data.categoryId = this.data.categoryInfoList[this.data.categoryIndex].categoryId;
  },

  
})